﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameEditor.Common.InternalDialogs
{
    public partial class VectorEditDialog : Form
    {
        public String[] Items
        {
            get
            {
                string[] array = new string[this.listBox1.Items.Count];
                int i=0;
                foreach (object obj in this.listBox1.Items)
                {
                    array[i] = (string)this.listBox1.Items[i];
                    ++i;
                }
                return array;
            }
            set
            {
                foreach (string str in value)
                {
                    this.listBox1.Items.Add(str);
                }
            }
        }

        public string this[Int32 index]
        {
            get
            {
                if(index<0||index>=this.listBox1.Items.Count)
                {
                    throw new Exception("out of range");
                }
                return (string)this.listBox1.Items[index];
            }
            set
            {
                if (index < 0 )
                {
                    throw new Exception("out of range");
                }
                if (index < this.listBox1.Items.Count)
                    this.listBox1.Items[index] = value;
                else
                    this.listBox1.Items.Add(value);
            }
        }

        public VectorEditDialog()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }


        public void Add(string item)
        {
            this.listBox1.Items.Add(item);
        }

        

        public static DialogResult ShowMe<T>(ref T[] listT)
        {
            VectorEditDialog dialog = new VectorEditDialog();
            TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));
            foreach (T t in listT)
            {
                dialog.Add(tc.ConvertToString(t));
            }


            DialogResult result= dialog.ShowDialog();
            if(result==DialogResult.OK)
            {
                listT = new T[dialog.Items.Length];
                int i = 0;
                foreach (string str in dialog.Items)
                {
                    listT[i++]=((T)tc.ConvertFromString(str));
                }
            }
            return result;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(this.listBox1.SelectedIndex>=0)
            {
                this.listBox1.Items.RemoveAt(this.listBox1.SelectedIndex);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(this.textBox1.Text.Length>0)
            {
                this.listBox1.Items.Add(this.textBox1.Text);
                this.textBox1.Clear();             
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int selectInx = this.listBox1.SelectedIndex;
            if (selectInx > 0)
            {
                object o = this.listBox1.Items[selectInx];
                this.listBox1.Items.RemoveAt(selectInx);
                this.listBox1.Items.Insert(selectInx - 1, o);
                this.listBox1.SelectedIndex = selectInx - 1;
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            int selectInx = this.listBox1.SelectedIndex;
            if (selectInx >= 0 && selectInx < this.listBox1.Items.Count - 1)
            {
                object o = this.listBox1.Items[selectInx];
                this.listBox1.Items.RemoveAt(selectInx);
                this.listBox1.Items.Insert(selectInx + 1, o);
                this.listBox1.SelectedIndex = selectInx + 1;
            }
        }


    }
}
